﻿using MVCOA.Helper;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace MVCOA.Areas.Manage.Controllers
{
    public class Treedata
    {
        public int id { get;set;}
        public int pid { get; set; }
        public string text{get;set;}
    }
    public class SortTreeController : Controller
    {
        //data.push({ id: 1, pid: 0, text: '1' });
        //data.push({ id: 2, pid: 1, text: '1.1' });
        //data.push({ id: 4, pid: 2, text: '1.1.2' });
        //data.push({ id: 5, pid: 2, text: '1.1.2' });

        // GET: /Manage/SortTree/

        public ActionResult Index()
        {
            return View();
        }
    
        public ActionResult Getdata()
        {
            int count = 1;
            int Pcount = 1;
            int P2count = 1;
            List<Treedata> list = new List<Treedata>();
            List<MODEL.Cate_Fisrt> list1 = OperateContext.Current.BLLSession.ICate_FisrtBLL.GetListBy(q => true).Select(s => s.ToPOCO()).ToList();
            foreach (MODEL.Cate_Fisrt s in list1)
            {
                Treedata data = new Treedata() { id = count, pid = 0, text = s.name };
                list.Add(data);
                List<MODEL.Cate_Second> list2 = OperateContext.Current.BLLSession.ICate_SecondBLL.GetListBy(q => q.firstId==s.id).Select(s2 => s2.ToPOCO()).ToList();
                Pcount = count;
                foreach (MODEL.Cate_Second second in list2)
                {
                    count++;
                    Treedata data2 = new Treedata() { id = count, pid = Pcount, text = second.name };
                    list.Add(data2);
                   
                    List<MODEL.Cate_Third> list3 = OperateContext.Current.BLLSession.ICate_ThirdBLL.GetListBy(q => q.secondId == second.id).Select(s3 => s3.ToPOCO()).ToList();
                    P2count = count;
                    foreach (MODEL.Cate_Third third in list3)
                    {
                        count++;
                        Treedata data3 = new Treedata() { id = count, pid = P2count, text = third.name };
                        list.Add(data3);
                       
                    }
                   
                }
               
            }
            //List<MODEL.Cate_Second> list2 = OperateContext.Current.BLLSession.ICate_SecondBLL.GetListBy(q => true).Select(s => s.ToPOCO()).ToList();
            //foreach (MODEL.Cate_Second s in list2)
            //{
            //    Treedata data = new Treedata() { id = count, pid = 1, text = s.name };
            //    list.Add(data);
            //    count++;
            //}
            //List<MODEL.Cate_Third> list3 = OperateContext.Current.BLLSession.ICate_ThirdBLL.GetListBy(q => true).Select(s => s.ToPOCO()).ToList();
            //foreach (MODEL.Cate_Third s in list3)
            //{
            //    Treedata data = new Treedata() { id = count, pid = 2, text = s.name };
            //    list.Add(data);
            //    count++;
            //}
            return Json(list, JsonRequestBehavior.AllowGet);
        }
    }
}
